# --**coding:utf-8**--
# Create by author: hemq

'''
    封装db操作，获取所有db数据
'''
from channel.models import ChannelListConfig, Nodeconfig, Channellist, PreChannelDownload, HisChannelDownload
from config import configInfo

def get_360_info_channelNum():
    '''
    :return: 360加固包相关渠道号信息
    '''
    channel_360_num = []
    channel_360_info = ChannelListConfig.objects.filter(projectshortname='jdb', jiagu='360')
    for info in channel_360_info:
        channel_360_num.append(info.code)
    return channel_360_num


def get_360_info_channelName():
    '''
    :return: 360加固包相关应用市场信息
    '''
    channel_360_name = []
    channel_360_info = ChannelListConfig.objects.filter(projectshortname='jdb', jiagu='360')
    for info in channel_360_info:
        channel_360_name.append(info.name)
    return channel_360_name


def get_bd_info():
    '''
    :return: 百度加固包相关渠道号信息
    '''
    channel_baidu_num = []
    channel_baidu_info = ChannelListConfig.objects.filter(projectshortname='jdb', jiagu='bd')
    for info in channel_baidu_info:
        channel_baidu_num.append(info.code)
    return channel_baidu_num


def get_bd_channelName():
    '''
    :return: 百度加固包相关应用市场信息
    '''
    channel_baidu_name = []
    channel_baidu_info = ChannelListConfig.objects.filter(projectshortname='jdb', jiagu='bd')
    for info in channel_baidu_info:
        channel_baidu_name.append(info.name)
    return channel_baidu_name


def get_keyAlias(projectshortname, platform):
    '''
    :return: 根据传参判断app类型，返回对应app的keyAlias
    '''
    apk_keyAlias = ''
    for keyAlias in Nodeconfig.objects.filter(projectshortname=projectshortname, platform=platform):
        apk_keyAlias = keyAlias.keyalias
    return apk_keyAlias


def get_keyPassword(projectshortname, platform):
    '''
    :return: 根据传参判断app类型，返回对应app的keyPassword
    '''
    apk_keyPassword = ''
    for keyPassword in Nodeconfig.objects.filter(projectshortname=projectshortname, platform=platform):
        apk_keyPassword = keyPassword.keypassword
    return apk_keyPassword


def new_360_code():
    '''
    :return: 360 code
    '''
    allCode = []
    for code in Channellist.objects.filter(jiagu='360'):
        allCode.append(code.code)
    return allCode


def new_360_name():
    '''
    :return: 360 name
    '''
    allName = []
    for name in Channellist.objects.filter(jiagu='360'):
        allName.append(name.name)
    return allName


def new_bd_code():
    '''
    :return: bd code
    '''
    allCode = []
    for code in Channellist.objects.filter(jiagu='bd'):
        allCode.append(code.code)
    return allCode


def new_bd_name():
    '''
    :return: bd name
    '''
    allName = []
    for name in Channellist.objects.filter(jiagu='bd'):
        allName.append(name.name)
    return allName


def getSignInfo():
    """
    :return: sign info
    """
    sign = []
    for signInfo in Nodeconfig.objects.filter(projectshortname='jdb', platform='android'):
        sign.append(signInfo.keyalias)
        sign.append(signInfo.keypassword)
    return sign


def addUrl(version, channelname, channelcode, downloadurl, channel, downloadqrcode):
    """
    :return: add downl url result
    """
    try:
        preApkInfo = PreChannelDownload(version=version, channelname=channelname, channelcode=channelcode,
                                        downloadurl=downloadurl, channel=channel, downloadqrcode=downloadqrcode)
        preApkInfo.save()

    except Exception as ext:
        print 'ext'


def addHisUrl(version, channelname, channelcode, eachapkname, channel, downloadqrcode):
    """
    :return: add downl url result
    """
    # 拼接url
    downloadurl = configInfo.historyDownloadDir + version + '/jdb-sign-'+eachapkname
    try:
        preApkInfo = HisChannelDownload(version=version, channelname=channelname, channelcode=channelcode,
                                        downloadurl=downloadurl, channel=channel, downloadqrcode=downloadqrcode)
        preApkInfo.save()

    except Exception as ext:
       print 'ext'
